package main

import "fmt"

//#link:https://leetcode-cn.com/problems/circular-array-loop/
func main() {
	nums := []int{3, 1, 2}
	fmt.Println(circularArrayLoop(nums))
}

func circularArrayLoop(nums []int) bool {
	//从第一个下标开始遍历
	for i := 0; i < len(nums); i++ {
		//第一步 找环的起点,第二步判断是否全是正数或负数，第三步环的长度要大于1
		begin := i
		next := begin
		isPos := nums[begin] > 0
		//最多也就是走len(nums)步
		for step := 0; step < len(nums); step++ {
			next += nums[next]
			next %= len(nums)
			if next < 0 {
				next += len(nums)
			}
			if isPos != (nums[next] > 0) {
				break
			}
			//找到环的起点
			if next == begin {
				if step > 1 {
					return true
				}
				break
			}
		}
	}
	return false
}
